<?php

namespace app\api\controller;

use app\admin\model\Announcement as AnnouncementMdl;

use think\Db;
use think\Exception;
use think\Request;

class Announcement extends Base
{

    /**
     * 获取公告列表
     */
    public function getList()
    {
        $param = self::getHttpParam();

        $start = 0;
        $length = 20;
        if (!empty($param->start)) {
            $start = $param->start;
        }
        if (!empty($param->length)) {
            $length = $param->length;
        }
        $where = [];

        $model = new AnnouncementMdl();
        $fields = 'id, title, createTime';
        $order = 'createTime DESC';
        $result = $model->where($where)->field($fields)->limit($start, $length)->order($order)->select();
        $total = $model->where($where)->count();
        if (!$result) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '没有相关公告');
        }

        return showTotal(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $result, $total);
    }

    /**
     * 获取公告详情
     */
    public function getDetail()
    {
        $param = self::getHttpParam();

        if (empty($param->announcementId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'announcementId 不能这空');
        }

        $id = $param->announcementId;
        $where = [
            'id' => $id
        ];

        $model = new AnnouncementMdl();
        $fields = 'id, title, details, createTime';
        $item = $model->where($where)->field($fields)->find();

        if (empty($item)) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '未查到相关公告');
        } else {
            return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $item);
        }
    }
}